package Z06XianCheng.XC03TongBu;

import java.text.SimpleDateFormat;
import java.util.Date;

public class UserHeroSyn {
    public static String now() {
        return new SimpleDateFormat("HH:mm:ss").format(new Date());
    }

    public static void main(String[] args) {
        final Object someObject = new Object();
        final Hero graeen = new Hero();

        graeen.name = "盖伦";
        graeen.hp = 10000;

        int n = 1000;

        Thread[] addthreads = new Thread[n];
        Thread[] reducethreads = new Thread[n];

        for (int i = 0 ;i < n ;i++){
            Thread t = new Thread(){
                public void run(){
                    synchronized (graeen){
                        graeen.recover();
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();
            addthreads[i] = t;
        }

        for (int i = 0 ; i < n;i++){
            Thread t = new Thread(){
                public void run(){
                    synchronized (graeen){
                        graeen.hurt();
                    }

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();
            reducethreads[i] = t;
        }

        for (Thread t : addthreads){
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        for(Thread t : reducethreads){
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.printf("%d 个增加线程和 %d个减少线程结束后 %n 盖伦的血量是 %.0f%n",n,n,graeen.hp );

    }
}
